<?php

namespace App\Admin\Controllers;

use App\model\customer\CustomerBigDepart;
use App\Toutiao;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class JinriController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '今日头条配置';
    protected       $states = [
       'on'  => ['value' => 0, 'text' => '启用', 'color' => 'success'],
        'off' => ['value' => 1, 'text' => '禁用', 'color' => 'danger'],
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Toutiao());

        $grid->column('id', __('Id'));
        $grid->column('key', __('Key'));
        $grid->column('token', __('Token'));
        $grid->column('big_part_id','分组')->display(function ($value){
           return  CustomerBigDepart::find($value)->name ?? '';
        });

        $grid->column('is_display', '是否禁用')->switch($this->states);
        $grid->column('created_at', __('创建时间'));
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Toutiao::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('key', __('Key'));
        $show->field('token', __('Token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Toutiao());

        $form->text('name', __('备注' ))->required();
        $form->text('key', __('Key'))->required();
        $form->text('token', __('Token'))->required();
        $form->switch('is_display', __('启用禁用'))->states($this->states)->default(0);
        $form->select('big_part_id','分组')->options(CustomerBigDepart::all()->pluck('name','id'))->required();
        return $form;
    }
}
